package dnl.toolbox.files;

import java.io.File;
import java.io.IOException;

/**
 * 
 * @author Daniel Orr
 * 
 */
public class ArchiveUtils {

	private static Archiver archiver = new TruezipArchiver();
	
	/**
	 * Creates an archive.
	 * 
	 * @param sourceDir
	 *            this dir will be archived.
	 * @param target
	 *            the archive to create. The type of archive will match the
	 *            given file name.
	 * @param deleteSource
	 *            if <code>true</code>, the source directory will be deleted
	 *            after archiving.
	 * @throws IOException
	 */
	public static void createArchive(File sourceDir, File target,
			boolean deleteSource) throws IOException {
		archiver.createZip(sourceDir, target, deleteSource);
	}

	/**
	 * 
	 * @param sourceDir
	 * @param target
	 * @param deleteSource
	 * @throws IOException
	 */
	public static void appendToArchive(File sourceDir, File target,
			boolean deleteSource) throws IOException {
		createArchive(sourceDir, target, deleteSource);
	}

	public static void appendFileToArchive(File file, File archive,
			boolean deleteSource) throws IOException {
		if(!archive.exists()){
			de.schlichtherle.io.File f = new de.schlichtherle.io.File(archive);
			f.mkdir();
		}
		de.schlichtherle.io.File target = new de.schlichtherle.io.File(archive, file.getName());
		de.schlichtherle.io.File srcFile = new de.schlichtherle.io.File(file);
		srcFile.archiveCopyTo(target);
	}
	
	public static void unzip(File zipFile, File target, boolean deleteOrigin){
		try {
			archiver.unzip(zipFile, target, deleteOrigin);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
